<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/14
 * Time: 17:39
 */
namespace App\Http\Controllers\Api\V2_0_1;

use App\Http\Requests\Api\V2_0_1\RepaymentPlanRequest;
use App\Repositories\UserCreditCardRepository;
use App\Services\RepaymentPlanService;
use Illuminate\Http\Request;

class RepaymentPlanController extends BaseController
{
    protected $repaymentPlanService;
    protected $userCreditCardRepository;

    public function __construct(RepaymentPlanService $repaymentPlanService, UserCreditCardRepository $userCreditCardRepository)
    {
        parent::__construct();
        $this->repaymentPlanService = $repaymentPlanService;
        $this->userCreditCardRepository = $userCreditCardRepository;
    }

    /**
     * 提交还款计划
     */
    public function confirmRepaymentPlan(RepaymentPlanRequest $repaymentPlanRequest)
    {
        if (!empty($repaymentPlanRequest->consume_address)) {
            $consumeAddress = json_decode($repaymentPlanRequest->consume_address, true);
            if (empty($consumeAddress['province']) || empty($consumeAddress['province_code']) || empty($consumeAddress['city']) ||empty($consumeAddress['city_code'])
                || empty($consumeAddress['area']) || empty($consumeAddress['area_code'])) {
                return $this->responseJson(10524);//消费地址格式不正确
            }
        } else {
            return $this->responseJson(422, [], false, '消费地址不能为空', '消费地址不能为空');//消费地址不能为空
        }
        //判断是否有绑定
        $creditCard = $this->userCreditCardRepository->validateCreditCardIsUser($repaymentPlanRequest->id, $this->user['user_id']);
        if (empty($creditCard->bank_no) || 0 == $creditCard->is_bind) {
            return $this->responseJson(10306);//信用卡不存在
        }
        if (empty($creditCard->expire_date) || empty($creditCard->cvn2)) {
            if (empty($repaymentPlanRequest->expire_date) || empty($repaymentPlanRequest->cvn2)) {
                return $this->responseJson(10517);//信用卡认证
            } else {
                $expireDate = $repaymentPlanRequest->expire_date;
                $cvn2 = $repaymentPlanRequest->cvn2;
                $creditCard->update([
                    'expire_date' => $expireDate,
                    'cvn2' => $cvn2,
                    'update_time' => time()
                ]);
            }
        } else {
            $expireDate = $creditCard->expire_date;
            $cvn2 = $creditCard->cvn2;
        }
        return $this->repaymentPlanService->makePlan($this->user, $repaymentPlanRequest->id,
            $repaymentPlanRequest->repayment_plan_data, $expireDate, $cvn2, $repaymentPlanRequest->consume_address);
    }

    /**
     * 还款计划详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function repaymentPlanDetail(Request $request)
    {
        if (empty($request->plan_id)) {
            return $this->responseJson(422);
        }
        return $this->repaymentPlanService->repaymentPlanDetail($request->plan_id, $this->user['user_id']);
    }

    /**
     * 更新计划状态为停止
     */
    public function updatePlanStatusToStop(Request $request)
    {
        if (empty($request->plan_id)) {
            return $this->responseJson(422);
        }
        return $this->repaymentPlanService->updatePlanStatusToStop($request->plan_id, $this->user['user_id']);
    }

    /**
     * 计划继续执行
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updatePlanStatusToContinue(Request $request)
    {
        if (empty($request->plan_id)) {
            return $this->responseJson(422);
        }
        return $this->repaymentPlanService->updatePlanStatusToContinue($request->plan_id, $this->user, $request->expire_date, $request->cvn2);
    }

    /**
     * 删除计划
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        if (empty($request->plan_id)) {
            return $this->responseJson(422);
        }
        return $this->repaymentPlanService->delete($request->plan_id, $this->user['user_id']);
    }
}